<?php

namespace app\web\controller;

use think\Db;
use think\Request;
use think\Validate;
use app\model\Modules;

class Yxmodule extends Base
{

    public function yxmodule()
    {
        //http://d5.dev.yixuan.net/web/index.php?c=site&a=entry&do=yxmodule&m=qq19064&op=display&themeid=1
        global $_G, $_P;
        $do = 'yxmodule';
        $op = !empty($_P['op']) ? $_P['op'] : 'display';
        switch ($op) {
            case 'display':
                return $this->lists();
                break;
            case 'search':
                return $this->search();
                break;
            case 'add_modules':
                return $this->addModules();
                break;
            default:
                return $this->lists();
                break;
        }
    }

    public function search()
    {
        global $_G, $_P;
        if (!$_G['isfounder']) {
            return_msg('无权限操作!');
        }

        $softname = trim($_P['keyword']);
        if (strlen($softname) <= 3 && $softname == intval($softname)) {
            $server = self::getServerDomain($softname);
            $url = "https://" . $server . "/web/index.php?c=site&a=entry&do=yxmodule&op=dispaly";
            echo "<script language='javascript' type='text/javascript'>";
            echo "window.location.href='$url';";
            echo "</script>";
            exit;
        }
        if (strpos($softname, '帐户') !== false) {
            $softname = substr($softname, strpos($softname, '帐户'), 20);
        }
        if (strpos($softname, '.aspx') !== false) {
            $softname = substr($softname, strpos($softname, '/') + 2);
            $softname = substr($softname, strpos($softname, '/'), 20);
        }
        if (strpos($softname, '&m=') !== false) {
            $softname = substr($softname, strpos($softname, '&m='));
        }

        $softname = intval(self::findNum($softname));
        if ($softname < 1000) {
            $softname = '00' . $softname;
        } elseif ($softname < 10000) {
            $softname = '0' . $softname;
        }

        $now_server = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], "."));
        if (strlen($softname) == 5) {
            $new_server = substr($softname, 0, 2);
//            if (empty($_P['isold'])) {
//                if (intval($new_server) < 10 || $new_server == '10' || $new_server == '11' || $new_server == '13' || $new_server == '15' || $new_server == '16' || $new_server == '23' || $new_server == '27' || $new_server == '28' || $new_server == '32' || $new_server == '35' || $new_server == '36' || $new_server == '38') {
//                    $new_server = 100 + intval($new_server);
//                }
//            }
        } elseif (strlen($softname) == 6) {
            $new_server = substr($softname, 0, 3);
//            $softname = ;
        } else {
            return_msg('请输入正确的活动编号:' . $softname);
        }
        $softname_search = $new_server > 100 && strlen($softname) == 6 ? substr($softname, 1) : $softname;
        $sql = "SELECT module_name FROM yx_sys_modules WHERE module_name  like '%" . $softname_search . "' group by module_name";
        $module = db_select($sql);
        if (count($module) > 1) {
            $url = "https://" . $_SERVER['HTTP_HOST'] . "/web/index.php?c=site&a=entry&do=yxmodule&op=dispaly&keyword=" . $softname;
            echo "<script language='javascript' type='text/javascript'>";
            echo "window.location.href='$url';";
            echo "</script>";
        } elseif (count($module) == 1) {
            $url = "https://" . $_SERVER['HTTP_HOST'] . "/web/index.php?c=site&a=entry&do=theme&op=display&cw=home&m=" . $module[0]['module_name'];
            echo "<script language='javascript' type='text/javascript'>";
            echo "window.location.href='$url';";
            echo "</script>";
        } else {
            if ($new_server != $now_server) {
                //跳转至对应服务器
                $server = self::getServerDomain($new_server);
                $url = "https://" . $server . "/web/index.php?c=site&a=entry&do=yxmodule&op=search&keyword=" . $softname;
                echo "<script language='javascript' type='text/javascript'>";
                echo "window.location.href='$url';";
                echo "</script>";
            } else {
                return_msg('未找到活动编号:' . $softname);
            }
        }
    }

    function getServerDomain($new_server)
    {
        if ($new_server == 37) {
            $server = $new_server . ".jince.cc";
        } elseif ($new_server == 61 || $new_server == 90) {
            $server = $new_server . ".lingze.net";
        } elseif ($new_server == 111) {
            $server = $new_server . ".liandeng.com";
        } elseif ($new_server == 20) {
            $server = $new_server . ".xjpf.net";
        } elseif ($new_server == 36 || $new_server == 38 || $new_server == 88 || $new_server == 89 || $new_server == 109 || $new_server == 110) {
            $server = $new_server . ".feichi.net";
        } elseif ($new_server > 100) {
            $server = $new_server . ".yixuan.cc";
        } else {
            $server = $new_server . ".yixuan.net";
        }
        return $server;
    }

    public function findNum($str = '')
    {
        $str = trim($str);
        if (empty($str)) {
            return '';
        }
        $result = '';
        for ($i = 0; $i < strlen($str); $i++) {
            if (is_numeric($str[$i])) {
                $result .= $str[$i];
            } elseif (!empty($result)) {
                break;
            }
        }
        return $result;
    }

    public function lists()
    {
        global $_G, $_P;
        $where = '';
        $do = 'yxmodule';
        $op = 'display';
        $param = input('param.');
        if (isset($param['keyword']) && $param['keyword']) {
            $keyword = $param['keyword'];
        }

        $datatype = isset($param['datatype']) && $param['datatype'] ? $param['datatype'] : 'all';
        $softmode = isset($param['softmode']) && $param['softmode'] ? $param['softmode'] : '';
        $deafult_sort = $datatype == 'all' ? 'update_time' : 'id';
        if ($datatype == 'void_num') {
            $sort = 'right(module_name,5) asc';
        } else if ($datatype == 'going_num') {
            $sort = 'right(module_name,5) desc';
        } else {
            $sort = isset($param['sort']) && $param['sort'] ? $param['sort'] : $deafult_sort;
            $sort .= ' desc';
        }

        $pageSize = 20;
        $pageNumber = max(1, intval($_P['page']));
        $modules = new Modules();
        $nowTime = time();
        $all_data = $modules->field(['count(*) as total',
            "count(case when json_unquote(json_extract(theme, '$.endtime')) > {$nowTime} and json_unquote(json_extract(theme, '$.status')) >=0 then 1 end) as going_num",
            "count(case when json_unquote(json_extract(theme,'$.title')) is null then 1 end) as void_num"
        ])->find();

        $all_soft_type = $modules->getModulesTypeData();
        //查询所有活动列表标号
        $all_module = $modules->order('id', 'desc')->column('distinct module_name');

        $list = $modules->getList($sort);

        $total = $list->total();

        $pager = pagination($total, $pageNumber, $pageSize);

        $is_yixuan = 1;
        if (DOMAIN1 == 'www.yixuan.net') {
            $is_yixuan = 0;
        }

        return view('/yxmodule', [
            '_G' => $_G,
            '_P' => $_P,
            'list' => $list,
            'pager' => $pager,
            'total' => $total,
            'keyword' => $keyword,
            'datatype' => $datatype,
            'sort' => $sort,
            'do' => $do,
            'op' => $op,
            'pageNumber' => $pageNumber,
            'pageSize' => $pageSize,
            'all_data' => $all_data,
            'all_module' => $all_module,
            'all_soft_type' => $all_soft_type,
            'softmode' => $softmode,
            'is_yixuan' => $is_yixuan,
        ]);

    }

    public function addModules()
    {
        global $_G, $_P;
        $modules = new Modules();
        $addRes = $modules->addModules();
        return $addRes;
    }
}